package com.stylefeng.guns.rest.modular.cinema.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.MeetAreaDict;
import com.stylefeng.guns.rest.common.persistence.model.MeetBrandDict;
import com.stylefeng.guns.rest.common.persistence.model.MeetCinema;
import com.stylefeng.guns.rest.common.persistence.model.MeetHallDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Service(interfaceClass = CinemaServiceAPI.class, executes = 10)
public class DefaultCinemaServiceImpl implements CinemaServiceAPI {

    @Autowired
    private MeetCinemaMapper meetCinemaMapper;
    @Autowired
    private MeetAreaDictMapper meetAreaDictMapper;
    @Autowired
    private MeetBrandDictMapper meetBrandDictMapper;
    @Autowired
    private MeetHallDictMapper meetHallDictMapper;
    @Autowired
    private MeetHallFilmInfoMapper meetHallFilmInfoMapper;
    @Autowired
    private MeetFieldMapper meetFieldMapper;


    //1、根据CinemaQueryVo，查询影院列表
    @Override
    public Page<CinemaVo> getCinemas(CinemaQueryVo cinemaQueryVo) {
        // 业务实体集合
        List<CinemaVo> cinemas = new ArrayList<>();

        Page<MeetCinema> page = new Page<>(cinemaQueryVo.getNowPage(), cinemaQueryVo.getPageSize());
        // 判断是否传入查询条件 -> brandId,distId,hallType 是否==99
        EntityWrapper<MeetCinema> entityWrapper = new EntityWrapper<>();
        if (cinemaQueryVo.getBrandId() != 99) {
            entityWrapper.eq("brand_id", cinemaQueryVo.getBrandId());
        }
        if (cinemaQueryVo.getDistrictId() != 99) {
            entityWrapper.eq("area_id", cinemaQueryVo.getDistrictId());
        }
        if (cinemaQueryVo.getHallType() != 99) {  // %#3#%
            entityWrapper.like("hall_ids", "%#+" + cinemaQueryVo.getHallType() + "+#%");
        }

        // 将数据实体转换为业务实体
        List<MeetCinema> meetCinemas = meetCinemaMapper.selectPage(page, entityWrapper);
        for (MeetCinema meetCinema : meetCinemas) {
            CinemaVo cinemaVo = new CinemaVo();

            cinemaVo.setUuid(meetCinema.getUuid() + "");
            cinemaVo.setMinimumPrice(meetCinema.getMinimumPrice() + "");
            cinemaVo.setCinemaName(meetCinema.getCinemaName());
            cinemaVo.setAddress(meetCinema.getCinemaAddress());

            cinemas.add(cinemaVo);
        }

        // 根据条件，判断影院列表总数
        long counts = meetCinemaMapper.selectCount(entityWrapper);

        // 组织返回值对象
        Page<CinemaVo> result = new Page<>();
        result.setRecords(cinemas);
        result.setSize(cinemaQueryVo.getPageSize());
        result.setTotal(counts);

        return result;
    }

    //2、根据条件获取品牌列表[除了就99以外，其他的数字为isActive]
    @Override
    public List<BrandVo> getBrands(int brandId) {
        boolean flag = false;
        List<BrandVo> brandVos = new ArrayList<>();
        // 判断brandId是否存在
        MeetBrandDict meetBrandDict = meetBrandDictMapper.selectById(brandId);
        // 判断brandId 是否等于 99
        if (brandId == 99 || meetBrandDict == null || meetBrandDict.getUuid() == null) {
            flag = true;
        }
        // 查询所有列表
        List<MeetBrandDict> meetBrandDictts = meetBrandDictMapper.selectList(null);
        // 判断flag如果为true，则将99置为isActive
        for (MeetBrandDict brand : meetBrandDictts) {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandName(brand.getShowName());
            brandVo.setBrandId(brand.getUuid() + "");
            // 如果flag为true，则需要99，如为false，则匹配上的内容为active
            if (flag) {
                if (brand.getUuid() == 99) {
                    brandVo.setActive(true);
                }
            } else {
                if (brand.getUuid() == brandId) {
                    brandVo.setActive(true);
                }
            }

            brandVos.add(brandVo);
        }

        return brandVos;
    }

    //3、获取行政区域列表
    @Override
    public List<AreaVo> getAreas(int areaId) {
        boolean flag = false;
        List<AreaVo> areaVos = new ArrayList<>();
        // 判断brandId是否存在
        MeetAreaDict meetAreaDict = meetAreaDictMapper.selectById(areaId);
        // 判断brandId 是否等于 99
        if (areaId == 99 || meetAreaDict == null || meetAreaDict.getUuid() == null) {
            flag = true;
        }
        // 查询所有列表
        List<MeetAreaDict> meetAreaDicts = meetAreaDictMapper.selectList(null);
        // 判断flag如果为true，则将99置为isActive
        for (MeetAreaDict area : meetAreaDicts) {
            AreaVo areaVo = new AreaVo();
            areaVo.setAreaName(area.getShowName());
            areaVo.setAreaId(area.getUuid() + "");
            // 如果flag为true，则需要99，如为false，则匹配上的内容为active
            if (flag) {
                if (area.getUuid() == 99) {
                    areaVo.setActive(true);
                }
            } else {
                if (area.getUuid() == areaId) {
                    areaVo.setActive(true);
                }
            }

            areaVos.add(areaVo);
        }

        return areaVos;
    }

    //4、获取影厅类型列表
    @Override
    public List<HallTypeVo> getHallTypes(int hallType) {
        boolean flag = false;
        List<HallTypeVo> hallTypeVos = new ArrayList<>();
        // 判断brandId是否存在
        MeetHallDict meetHallDict = meetHallDictMapper.selectById(hallType);
        // 判断brandId 是否等于 99
        if (hallType == 99 || meetHallDict == null || meetHallDict.getUuid() == null) {
            flag = true;
        }
        // 查询所有列表
        List<MeetHallDict> meetHallDicts = meetHallDictMapper.selectList(null);
        // 判断flag如果为true，则将99置为isActive
        for (MeetHallDict hall : meetHallDicts) {
            HallTypeVo hallTypeVo = new HallTypeVo();
            hallTypeVo.setHalltypeName(hall.getShowName());
            hallTypeVo.setHalltypeId(hall.getUuid() + "");
            // 如果flag为true，则需要99，如为false，则匹配上的内容为active
            if (flag) {
                if (hall.getUuid() == 99) {
                    hallTypeVo.setActive(true);
                }
            } else {
                if (hall.getUuid() == hallType) {
                    hallTypeVo.setActive(true);
                }
            }

            hallTypeVos.add(hallTypeVo);
        }

        return hallTypeVos;
    }

    //5、根据影院编号，获取影院信息
    @Override
    public CinemaInfoVo getCinemaInfoById(int cinemaId) {
        MeetCinema meetCinema = meetCinemaMapper.selectById(cinemaId);
        if (meetCinema == null) {
            return new CinemaInfoVo();
        }
        CinemaInfoVo cinemaInfoVo = new CinemaInfoVo();
        cinemaInfoVo.setImgUrl(meetCinema.getImgAddress());
        cinemaInfoVo.setCinemaPhone(meetCinema.getCinemaPhone());
        cinemaInfoVo.setCinemaName(meetCinema.getCinemaName());
        cinemaInfoVo.setCinemaId(meetCinema.getUuid() + "");
        cinemaInfoVo.setCinemaId(meetCinema.getCinemaAddress());

        return cinemaInfoVo;
    }

    //6、获取所有电影的信息和对应的放映场次信息，根据影院编号
    @Override
    public List<FilmInfoVo> getFilmInfoByCinemaId(int cinemaId) {
        List<FilmInfoVo> filmInfos = meetFieldMapper.getFilmInfos(cinemaId);

        return filmInfos;
    }

    //7、根据放映场次ID获取放映信息
    @Override
    public HallInfoVo getFilmFieldInfo(int fieldId) {
        HallInfoVo hallInfoVo = meetFieldMapper.getHallInfo(fieldId);

        return hallInfoVo;
    }

    //8、根据放映场次查询播放的电影编号，然后根据电影编号获取对应的电影信息
    @Override
    public FilmInfoVo getFilmInfoByFieldId(int fieldId) {
        FilmInfoVo filmInfoVo = meetFieldMapper.getFilmInfoById(fieldId);

        return filmInfoVo;
    }
}
